/**
 * Copyright(c) 2015 BASUNG Techonologies Co., Ltd.
 * <p/>
 * History:
 * 15/8/28 下午7:29 Created by leijinghan
 */
package com.jade.bss.db.impl;

import com.jade.bss.DbException;
import com.jade.bss.db.IBaseDao;
import com.jade.bss.db.IBaseManager;
import com.jade.framework.base.criterion.Criteria;
import com.jade.framework.base.util.PagingList;
import org.springframework.transaction.annotation.Transactional;

import javax.ejb.TransactionAttribute;
import javax.inject.Named;

/**
 * Explain: 通用数据管理接口实现
 *
 * @author <a href="mailto:jinghan.lei@basung.com">leijinghan</a>
 * @date 15/8/28 下午7:29
 * @version 1.0
 */
@Named("bss_baseManager")
public abstract class BaseManagerImpl<M extends java.io.Serializable, PK extends java.io.Serializable> implements IBaseManager<M,PK>
{
    public IBaseDao<M, PK> baseDao;

    public abstract void setBaseDao(IBaseDao<M, PK> baseDao);

    @TransactionAttribute
    @Transactional (rollbackFor = Exception.class)
    public void add(M model) throws DbException
    {
        baseDao.add(model);
    }

    @TransactionAttribute
    @Transactional (rollbackFor = Exception.class)
    public void update(M model) throws DbException
    {
        baseDao.update(model);
    }

    @TransactionAttribute
    @Transactional (rollbackFor = Exception.class)
    public void delete(Class<M> clazz, PK... ids) throws DbException
    {
        for (PK id : ids) {
            M model = baseDao.get(clazz, id);
            if (model != null) {
                baseDao.delete(model);
            }
        }
    }

    public M get(Class<M> clazz, PK id)
    {
        return baseDao.get(clazz, id);
    }

    public PagingList<M> listall(Class<M> clazz, Criteria criteria)
    {
        return baseDao.listall(clazz, criteria);
    }
}
